package graphiceditor;

import java.awt.Color;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

public enum ShapeType {

    LINE {

	@Override
	public void changeShapeBounds(float newX, float newY,
		ShapeWithColor shape) {
	    Line2D line = (Line2D) shape.getShape();
	    line.setLine(line.getX1(), line.getY1(), newX, newY);
	}

	@Override
	public String getName() {
	    return "Line";
	}

	@Override
	protected Shape doCrateShape(float x, float y) {
	    return new Line2D.Float(x, y, x, y);
	}

	@Override
	public boolean isValid(ShapeWithColor shape) {
	    return true;
	}
    },
    RECTANGLE {

	@Override
	public void changeShapeBounds(float newX, float newY,
		ShapeWithColor shape) {
	    Rectangle2D rect = (Rectangle2D) shape.getShape();
	    rect.setRect(rect.getX(), rect.getY(), newX - rect.getX(), newY
		    - rect.getY());

	}

	@Override
	public String getName() {
	    return "Rectangle";
	}

	@Override
	protected Shape doCrateShape(float x, float y) {
	    return new Rectangle2D.Float(x, y, 0, 0);
	}

	@Override
	public boolean isValid(ShapeWithColor shape) {
	    Rectangle2D rect = (Rectangle2D) shape.getShape();
	    return rect.getWidth() > 0 && rect.getHeight() > 0;
	}
    },
    ELLIPSE {

	@Override
	public void changeShapeBounds(float newX, float newY,
		ShapeWithColor shape) {
	    Ellipse2D ellipse = (Ellipse2D) shape.getShape();
	    ellipse.setFrame(ellipse.getX(), ellipse.getY(),
		    newX - ellipse.getX(), newY - ellipse.getY());
	}

	@Override
	public String getName() {
	    return "Ellipse";
	}

	@Override
	protected Shape doCrateShape(float x, float y) {
	    return new Ellipse2D.Float(x, y, 0, 0);
	}

	@Override
	public boolean isValid(ShapeWithColor shape) {
	    Ellipse2D ellipse = (Ellipse2D) shape.getShape();
	    return ellipse.getWidth() > 0 && ellipse.getHeight() > 0;
	}
    };

    protected abstract Shape doCrateShape(float x, float y);

    public ShapeWithColor createFigure(float x, float y, Color color) {
	return new ShapeWithColor(doCrateShape(x, y), color);
    }

    public abstract void changeShapeBounds(float newX, float newY,
	    ShapeWithColor shape);

    public abstract String getName();

    public abstract boolean isValid(ShapeWithColor shape);
}
